package mulMan.domain;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Observable;
import java.util.Observer;


public class ListOfAudioFiles extends Observable implements List<AudioFileModel>, Observer {
	
	private List<AudioFileModel> list;
	
	public ListOfAudioFiles() {
		list = new ArrayList<AudioFileModel>();
	}
	
	
	
	public AudioFileModel remove(int index) {
		AudioFileModel result = list.remove(index);
		System.out.println("Observers: " + result.countObservers());
		result.deleteObserver(this);
		System.out.println("Observers: " + result.countObservers());
		setChanged();
		notifyObservers(result);

		return result;
	}

	public boolean add(AudioFileModel e) {
		boolean result = list.add(e);
		e.addObserver(this);
		
		if (e.getTitle().equals("t")) {
			System.out.println("New Observer for t");
		}
		
		setChanged();
		notifyObservers();
		return result;
	}

	public void add(int index, AudioFileModel element) {
		element.addObserver(this);
		setChanged();
		notifyObservers();
		list.add(index, element);
	}

	public boolean addAll(Collection<? extends AudioFileModel> c) {
		boolean result = list.addAll(c);
		for (AudioFileModel e : c) {
			e.addObserver(this);
		}
		setChanged();
		notifyObservers();
		return result;
	}

	public boolean addAll(int index, Collection<? extends AudioFileModel> c) {
		boolean result = list.addAll(index, c);
		for (AudioFileModel e : c) {
			e.addObserver(this);
		}
		setChanged();
		notifyObservers();
		return result;
	}

	public void clear() {
		list.clear();
	}

	public boolean contains(Object o) {
		return list.contains(o);
	}

	public boolean containsAll(Collection<?> c) {
		return list.containsAll(c);
	}

	public AudioFileModel get(int index) {
		return list.get(index);
	}

	public int indexOf(Object o) {
		return list.indexOf(o);
	}

	public boolean isEmpty() {
		return list.isEmpty();
	}

	public Iterator<AudioFileModel> iterator() {
		return list.iterator();
	}

	public int lastIndexOf(Object o) {
		return list.lastIndexOf(o);
	}

	public ListIterator<AudioFileModel> listIterator() {
		return list.listIterator();
	}

	public ListIterator<AudioFileModel> listIterator(int index) {
		return list.listIterator(index);
	}

	public boolean remove(Object o) {
		try {
			((AudioFileModel)o).deleteAudioFile();
		} catch (Exception e) {
			e.printStackTrace();
		}
		boolean result = list.remove(o);
		setChanged();
		notifyObservers(o);
		return result;
	}

	public boolean removeAll(Collection<?> c) {
		for (Object o : c) {
			try {
				((AudioFileModel)o).deleteAudioFile();
				setChanged();
				notifyObservers(o);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		boolean result = list.removeAll(c);
		setChanged();
		notifyObservers();
		return result;
	}

	public boolean retainAll(Collection<?> c) {
		return list.retainAll(c);
	}

	public AudioFileModel set(int index, AudioFileModel element) {
		return list.set(index, element);
	}

	public int size() {
		return list.size();
	}

	public List<AudioFileModel> subList(int fromIndex, int toIndex) {
		return list.subList(fromIndex, toIndex);
	}

	public Object[] toArray() {
		return list.toArray();
	}

	public <T> T[] toArray(T[] a) {
		return list.toArray(a);
	}



	public void update(Observable o, Object arg) {
		System.out.println("10");
		if ((o instanceof AudioFileModel) && (arg.equals("id"))) {
			System.out.println("11");
			if (this.contains(o)) {
				System.out.println("12");
				this.remove(indexOf(o));
			}
		}
	}
}
